package jsyf.oa.dao.system;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.system.TargetEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by WHH-PC on 2018/1/26.
 */
@Repository
public class TargetDao extends BaseDao {

    public TargetDao() {
        super.iBaseEntity = new TargetEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, TargetEntity.class);
    }

    public void insertBatch(List<TargetEntity> targetList){
        List<LinkedHashMap<String,Object>> insertList = new ArrayList<LinkedHashMap<String,Object>>();
        LinkedHashMap<String,Object> targetMap = null;
        for (int i = 0; i < targetList.size(); i++) {
            TargetEntity targetEntity = targetList.get(i);
            targetMap = new LinkedHashMap<>();
            targetMap.put("ID",super.generateKey());
            targetMap.put("COMPANY_ID",targetEntity.getCompanyId());
            targetMap.put("USER_ID",targetEntity.getUserId());
            targetMap.put("TARGET_DATE",targetEntity.getTargetDate());
            targetMap.put("FACE_TARGET",targetEntity.getFaceTarget());
            targetMap.put("TEL_TARGET",targetEntity.getTelTarget());
            targetMap.put("CREATE_USER_ID",targetEntity.getCreateUserId());
            targetMap.put("REGION_ID",targetEntity.getRegionId());
            targetMap.put("BIG_REGION_ID",targetEntity.getBigRegionId());
            targetMap.put("CREATE_TIME",this.getCurrentTime());
            insertList.add(targetMap);
        }
        super.insertBatch("T_SYS_TARGET",insertList);
    }

    public List<JSONObject> getCompanyTarget(){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT a.`NAME` as companyName,b.faceTarget,b.telTarget,b.companyId FROM t_sys_company as a ");
        sb.append("LEFT JOIN (SELECT SUM(FACE_TARGET) as faceTarget,SUM(TEL_TARGET) as telTarget, ");
        sb.append("COMPANY_ID as companyId FROM t_sys_target WHERE TARGET_DATE=curdate() GROUP BY COMPANY_ID) as b on a.id=b.companyId ");
        sb.append("WHERE a.PARENT_ID<>'0' and a.DEL_FLAG<>'1' AND a.`NAME` NOT LIKE '%大客户%'  ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString());
        return jsonList;
    }

}
